
#include "Timer.h"
#include <SDL/SDL.h> 
#include <iostream>

// the number of  milliseconds that passed between Initialisation and the last frame render.
static unsigned int time_of_last_frame = 0;

// Number of seconds and milliseconds that tha last frame took to resolve.
static float seconds_since_last_frame = 0.0f;
static unsigned int mseconds_since_last_frame = 0;
static unsigned long num_frames_rendered = 0;
static unsigned int start_time;

void Timer::initialise(void){
	start_time = time_of_last_frame = SDL_GetTicks();
	update();
}


void Timer::update(void){
	num_frames_rendered++;
	mseconds_since_last_frame = SDL_GetTicks() - time_of_last_frame;
//    if(mseconds_since_last_frame == 0){ mseconds_since_last_frame = 1; }
    
	time_of_last_frame += mseconds_since_last_frame;

//    if(mseconds_since_last_frame > 30){ mseconds_since_last_frame = 30; }
	seconds_since_last_frame = mseconds_since_last_frame * 0.001f;
    if(seconds_since_last_frame > 1.0f/20.0f){ seconds_since_last_frame = 1.0f/20.0f; }
}

unsigned int Timer::currentTicks(void){ 
    return SDL_GetTicks(); 
}

float Timer::secondsSinceLastFrame(void){
	return seconds_since_last_frame;
}

unsigned int Timer::millisecondsSinceLastFrame(void){
	return mseconds_since_last_frame;
}

float Timer::queryAverageFPS(void){
	return (float)num_frames_rendered/(float)(SDL_GetTicks() - start_time) * 1000.0f;
}
